package com.daocao.yfb.fragment;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.daocao.yfb.R;
import com.daocao.yfb.activity.LoginActivity;
import com.daocao.yfb.activity.WebViewActivity;
import com.daocao.yfb.api.Api;
import com.daocao.yfb.api.bean.CodeBean;
import com.daocao.yfb.constant.Const;
import com.daocao.yfb.util.Network;
import com.daocao.yfb.util.RxLifeUtil;
import com.daocao.yfb.util.ToastUtil;
import com.daocao.yfb.util.UI;


//import cn.sharesdk.framework.Platform;
//import cn.sharesdk.framework.PlatformActionListener;
//import cn.sharesdk.framework.PlatformDb;
//import cn.sharesdk.framework.ShareSDK;
//import cn.sharesdk.sina.weibo.SinaWeibo;
//import cn.sharesdk.tencent.qq.QQ;
//import cn.sharesdk.wechat.friends.Wechat;

/**
 * Created by xsooy-pc on 17-12-4.
 */
//implements PlatformActionListener, Handler.Callback
public class LoginByPhoneFragment extends BaseFragment {
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.btn_code)
    TextView btnCode;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    Unbinder unbinder;
    @BindView(R.id.btn_qq)
    ImageView btnQq;
    @BindView(R.id.btn_wechat)
    ImageView btnWechat;
    @BindView(R.id.btn_weibo)
    ImageView btnWeibo;
    @BindView(R.id.txt_rule)
    TextView txtRule;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_login_phone;
    }

    @Override
    protected void initView() {
        UI.setTvHtml(txtRule, R.string.txt_rule_login);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (this.timer != null) {
            this.timer.cancel();
            timer = null;
        }

        unbinder.unbind();
        unbinder = null;
    }

    public void getCode(String user) {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }
        CodeBean item = new CodeBean();
        item.phone = user;

        Api.api_service.getCode(item).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {

                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    timer.start();
                    ToastUtil.makeText(getContext(), "发送成功，请注意查收");
                });
    }


    private CountDownTimer timer = new CountDownTimer(60000L, 1000L) {
        public void onFinish() {
            resetBtn();
        }

        @TargetApi(16)
        public void onTick(long paramAnonymousLong) {
            btnCode.setEnabled(false);
            btnCode.setText(paramAnonymousLong / 1000L + "s后重发");
        }
    };

    @TargetApi(16)
    private void resetBtn() {
        if (this.timer != null)
            this.timer.cancel();

//        if (this.isSend)
//            this.btnSendCode.setText(getResources().getString(R.string.send_code_again));
//        else
        btnCode.setText("发送");

        btnCode.setEnabled(true);
    }

    @OnClick({R.id.btn_code, R.id.btn_submit, R.id.btn_qq, R.id.btn_wechat, R.id.btn_weibo, R.id.txt_rule})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_qq:
//                setPlat(QQ.NAME);
                break;
            case R.id.txt_rule:
//                Intent intent = new Intent(getActivity(), WebViewActivity.class);
//                intent.putExtra("keyword", Const.ContractType.REGISTER);
//                startActivity(intent);
//                getActivity().overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
            case R.id.btn_wechat:

//                setPlat(Wechat.NAME);
                break;
            case R.id.btn_weibo:
//                setPlat(SinaWeibo.NAME);
                break;
            case R.id.btn_code:
                if (TextUtils.isEmpty(UI.toString(editPhone))) {
                    ToastUtil.makeText(getContext(), "手机号不能为空");
                    return;
                }
                getCode(UI.toString(editPhone));
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editPhone))) {
                    ToastUtil.makeText(getContext(), "手机号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editCode))) {
                    ToastUtil.makeText(getContext(), "验证码不能为空");
                    return;
                }
                ((LoginActivity) getActivity()).getLoginBySms(UI.toString(editPhone), UI.toString(editCode));
                break;
        }
    }


//    private void setPlat(String name) {
//        Platform weibo = ShareSDK.getPlatform(name);
//        if (weibo.isAuthValid()) {
//            weibo.removeAccount(true);
////            PlatformDb platDB = weibo.getDb();
////            ((LoginActivity) getActivity()).getQuickLogin(platDB.getUserId(), name);
////            return;
//        }
//
//
////回调信息，可以在这里获取基本的授权返回的信息，但是注意如果做提示和UI操作要传到主线程handler里去执行
//        weibo.SSOSetting(false);
//        weibo.setPlatformActionListener(new PlatformActionListener() {
//
//            @Override
//            public void onError(Platform arg0, int arg1, Throwable arg2) {
//                // TODO Auto-generated method stub
////                Log.i("dasdadsasda", arg2.toString());
//                arg2.printStackTrace();
////                ToastUtil.makeText(getActivity(), arg2.toString());
//            }
//
//            @Override
//            public void onComplete(Platform platform, int action, HashMap<String, Object> arg2) {
//                // TODO Auto-generated method stub
//                //输出所有授权信息
////                        platform.getDb().exportData();
//
//                getActivity().runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
////                        Log.i("dasdadsasda", "action=" + action);
//                        if (action == Platform.ACTION_USER_INFOR) {
//                            PlatformDb platDB = platform.getDb();//获取数平台数据DB
//
//                            ((LoginActivity) getActivity()).getQuickLogin(platDB.getUserId(), name);
//
//                        }
//                    }
//                });
//
//
//            }
//
//            @Override
//            public void onCancel(Platform arg0, int arg1) {
//                // TODO Auto-generated method stub
//
//            }
//        });
//        weibo.authorize();
//        weibo.showUser(null);//执行登录，登录后在回调里面获取用户资料
//    }

}
